package com.example.ixform.controllers;

import com.example.ixform.services.IdolService;
import com.example.ixform.services.ProductionService;
import com.example.ixform.entities.Idol;
import com.example.ixform.entities.Production;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("productions")
public class ProductionController {

    @Autowired
    ProductionService productionService;

    @Autowired
    IdolService idolService;


    @GetMapping
    public String displayProductions(Model model){
       List<Production> productions = productionService.getAll();
        model.addAttribute("productions",productions);
        return"productions/list-productions";
    }

    @GetMapping("/new")
    public String displayProductionForm(Model model){

        Production aProduction = new Production();
        List<Idol> idols = idolService.getAll();
        model.addAttribute("production",aProduction);
        model.addAttribute("allIdols",idols);
        return "productions/new-production";
    }

    @PostMapping("/save")
    public String createProduction(Model model, @RequestParam List<Long> idols, Production production){
        //处理保存到数据库的行为
        productionService.save(production);

        //使用重定位防止重复提交
        return "redirect:/productions";
    }
}